package com.data.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.data.dto.DataInfoQueryDTO;
import com.data.entity.DataInfo;
import com.data.service.DataInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/data-info")
public class DataInfoController {
    @Autowired
    private DataInfoService dataInfoService;
    /**
     * 获取字典选项：根据 dictType 返回 source/keyword1/keyword2 去重列表
     * @param dictType 1=来源(source), 2=类型(keyword1), 3=领域(keyword2)
     * @return ResponseEntity<List<String>> 返回字符串列表
     */
    @GetMapping("/options")
    public ResponseEntity<List<String>> getDictOptions(@RequestParam Integer dictType) {
        // 参数校验
        if (dictType == null || dictType < 1 || dictType > 3) {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(dataInfoService.getDictOptions(dictType));
    }
    // 导入Excel
    @PostMapping("/import")
    public ResponseEntity<String> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            dataInfoService.importExcel(file);
            return ResponseEntity.ok("导入成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("导入失败: " + e.getMessage());
        }
    }

    // 分页查询
    @PostMapping("/list")
    public ResponseEntity<?> list(@RequestBody @Valid DataInfoQueryDTO query) {
        final IPage<DataInfo> page = dataInfoService.listByPage(query);
        return ResponseEntity.ok(page);
    }
}